#!/bin/sh
# PCP QA Test No. 1242
# Test libpcp multi-archive support using pmrep
#
# Copyright (c) 2017 Red Hat.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.python

$python -c "from pcp import pmapi" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python pcp pmapi module not installed"
$python -c "from collections import OrderedDict" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python collections OrderedDict module not installed"
which pmrep >/dev/null 2>&1 || _notrun "pmrep not installed"

_get_libpcp_config
$multi_archive_contexts || _notrun "Multi-archive support is not available"

status=1	# failure is the default!
trap "cd $here; rm -rf $tmp $tmp.*; exit \$status" 0 1 2 3 15

_filter()
{
    tmpx=`echo $tmp`
    sed -e 's/^Archive:.* Host:/ARCHIVE: Host:/' \
	-e "s|$tmpx|TMP|g"
}

# real QA test starts here
TOOL=pmrep
METRIC=network.interface.in.bytes
mkdir -p $tmp.archives

for DIR in archives/multi-xz archives/multi-xz-1k
do
    # Check that running pmrep on a set of archives performs identically
    # to running it on the archive created by combining those same archives
    # using pmlogextract.

    # First, run pmlogextract against the archives.
    IN="$DIR/20150508.11.44 $DIR/20150508.11.46 $DIR/20150508.11.50 $DIR/20150508.11.57"
    OUT=$tmp.44+46+50+57.pmlogextract
    rm -f $OUT.*
    echo "Running pmlogextract $IN $OUT" | _filter | tee $seq_full
    pmlogextract $IN $OUT 2>&1 >> $seq_full
    [ $? -eq 0 ] || echo "Error running pmlogextract $IN $OUT -- see $seq.full"

    # Now run pmrep against the extracted archive.
    IN=$OUT
    OUTBASE=$OUT.$TOOL.txt
    echo "Running $TOOL -a $IN" | _filter
    $TOOL -a $IN $METRIC 2>&1 | _filter | tee $OUTBASE

    # Now, run pmrep against the set of archives.
    IN=$DIR/20150508.11.44,$DIR/20150508.11.46,$DIR/20150508.11.50,$DIR/20150508.11.57
    OUT=$tmp.44+46+50+57.$TOOL.txt
    echo "Running $TOOL -a $IN" | _filter
    $TOOL -a $IN $METRIC 2>&1 | _filter | tee $OUT

    # Make sure the output is the same
    echo "Running diff $OUTBASE $OUT" | _filter | tee -a $seq_full
    diff $OUTBASE $OUT 2>&1 >> $seq_full
    [ $? -eq 0 ] || echo "$OUT differs from $OUTBASE  -- see $seq.full"

    # Now do it again, specifying the set of archives using the directory name
    IN=$DIR
    OUT=$tmp.$DIR.$TOOL.txt
    echo "Running $TOOL -a $IN" | _filter
    $TOOL -a $IN $METRIC 2>&1 | _filter | tee $OUT

    # Make sure the output is the same
    echo "Running diff $OUTBASE $OUT" | _filter | tee -a $seq_full
    diff $OUTBASE $OUT 2>&1 >> $seq_full
    [ $? -eq 0 ] || echo "$OUT differs from $OUTBASE  -- see $seq.full"
done

# success, all done
status=0
exit
